<?php
/**
 * Registry.php
 *
 * 全局注册类
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 15:09:09
 * @modified    2023-02-14 15:09:09
 *
 */

namespace App\Library;

class Registry
{
    private static $instance;
    private $data = [];

    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public static function get($key, $default = null)
    {
        return self::getInstance()->getValue($key, $default);
    }

    public static function set($key, $value, $force = false)
    {
        if (self::getInstance()->has($key) && !$force) {
            return;
        }
        self::getInstance()->setValue($key, $value);
    }

    public function getValue($key, $default = null)
    {
        return $this->data[$key] ?? $default;
    }

    public function setValue($key, $value)
    {
        $this->data[$key] = $value;
    }

    public function has($key)
    {
        return isset($this->data[$key]);
    }
}